package com.zly.bscloud.service.impl;

import com.zly.bscloud.dto.GarbageDTO;
import com.zly.bscloud.entity.Garbage;
import com.zly.bscloud.mapper.GarbageMapper;
import com.zly.bscloud.service.GarbageService;
import com.zly.bscloud.sort.CSVUtil;
import com.zly.bscloud.sort.ChineseInital;
import com.zly.bscloud.sort.CompareFirst;
import com.zly.bscloud.utils.BeanHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author zhouxiaoyu
 * @Date 2020/5/6 9:20
 * @Version 1.0
 */
@Service
public class GarbageServiceImpl implements GarbageService {
    @Autowired
    private GarbageMapper garbageMapper;

    @Override
    @Transactional
    public List<GarbageDTO> showSort(){
        List<Garbage> garbageList = new ArrayList<Garbage>();
        try{
            garbageList = CSVUtil.readCSV();
        }catch (Exception e) {
            e.printStackTrace();
        }
        garbageList = CompareFirst.compareFist(garbageList);
        for(int i=0;i<garbageList.size();i++){
            String s = ChineseInital.getAllFirstLetter(garbageList.get(i).getName());
            char k=s.charAt(0);
            int first=k-'a';
            //if(first>=0&&first<27)
                //garbageList.get(i).setFirst(first);
            //else
                //garbageList.get(i).setFirst(26);
        }
        System.out.println(garbageList);
        List<GarbageDTO> garbageDTOList = BeanHelper.copyWithCollection(garbageList, GarbageDTO.class);
        return garbageDTOList;
    }

    @Override
    @Transactional
    public List<GarbageDTO> getSortIdByName(String name){
        // 创建Example
        Example example = new Example(Garbage.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.orLike("name","%"+name+"%");
        //criteria.andEqualTo("received", 0);
        System.out.println(example);
        List<Garbage> garbageList = garbageMapper.selectByExample(example);
        List<GarbageDTO> garbageDTOList = BeanHelper.copyWithCollection(garbageList, GarbageDTO.class);
        return garbageDTOList;

    }
}
